package cn.bs.ktv.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.Objects;

import cn.bs.ktv.R;
import cn.bs.ktv.adapter.SingerListAdapter;
import cn.bs.ktv.connection.ConnectionMgr;
import cn.bs.ktv.model.Constant;
import cn.bs.ktv.model.PagedModel;
import cn.bs.ktv.model.SingerModel;
import cn.bs.ktv.model.event.MsgEvent;
import cn.bs.ktv.model.input.GetSingerPagedListInput;

public class SingerListActivity extends AppCompatActivity implements View.OnClickListener {
    private final String logName = SingerListActivity.class.getName();

    private int currentPage = 1;
    private int maxPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singer_list);

        registerEvent();

        sendMsg();
    }

    @Override
    protected void onStart() {
        super.onStart();

        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MsgEvent event) {
        Log.i(logName, "the cmd is:" + event.getCmd());
        Log.i(logName, "the body is:" + event.getBody());

        if (!Constant.CMD.SINGER_LIST.equalsIgnoreCase(event.getCmd())) {
            return;
        }

        String body = event.getBody();
        if (StringUtils.isEmpty(body)) {
            return;
        }

        Type type = GsonUtils.getType(PagedModel.class, SingerModel.class);
        PagedModel<SingerModel> pagedModel = GsonUtils.fromJson(body, type);

        bindListView(pagedModel);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.btn_first_page:
                currentPage = 1;
                break;
            case R.id.btn_pre_page:
                if (currentPage > 1) {
                    currentPage--;
                } else {
                    currentPage = maxPage;
                }
                break;
            case R.id.btn_next_page:
                if (currentPage < maxPage) {
                    currentPage++;
                } else {
                    currentPage = 1;
                }
                break;
            case R.id.btn_last_page:
                currentPage = maxPage;
                break;
            default:
                break;
        }

        sendMsg();
    }

    private void registerEvent() {
        Button btn_first_page = findViewById(R.id.btn_first_page);
        Button btn_pre_page = findViewById(R.id.btn_pre_page);
        Button btn_next_page = findViewById(R.id.btn_next_page);
        Button btn_last_page = findViewById(R.id.btn_last_page);

        btn_first_page.setOnClickListener(this);
        btn_pre_page.setOnClickListener(this);
        btn_next_page.setOnClickListener(this);
        btn_last_page.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    private void bindListView(PagedModel<SingerModel> pagedModel) {
        if (Objects.isNull(pagedModel)) {
            return;
        }

        if (!pagedModel.hasData()) {
            return;
        }

        String currentPageString = getString(R.string.tip_current_page);
        TextView tv_current_page = findViewById(R.id.tv_current_page);
        tv_current_page.setText(currentPageString + pagedModel.getPageIndex());

        maxPage = pagedModel.getTotalPage();

        SingerListAdapter singerListAdapter = new SingerListAdapter(getApplicationContext(), pagedModel.getList());

        ListView lv_singer_list = findViewById(R.id.lv_singer_list);
        lv_singer_list.setOnItemClickListener((parent, view, position, id) -> {
            Object itemObj = parent.getItemAtPosition(position);

            if (itemObj instanceof SingerModel) {
                SingerModel model = (SingerModel) itemObj;

                Log.i(logName, "the singer name is:" + model.getSingerName());
            }
        });
        lv_singer_list.setAdapter(singerListAdapter);
    }

    private void sendMsg() {
        EditText et_singer_word = findViewById(R.id.et_singer_word);

        GetSingerPagedListInput input = new GetSingerPagedListInput();
        input.setPageIndex(currentPage);
        input.setPageSize(15);
        input.setKeyword(et_singer_word.getText().toString());

        ConnectionMgr.send(Constant.CMD.SINGER_LIST, input);
    }
}